package redis

import (
	"context"
	"encoding/json"
	"github.com/redis/go-redis/v9"
	"month_zg5/book_srv/common/global"
	"month_zg5/book_srv/models"
)

// 历史记录
func BookIncr(key string) error {
	return global.RedisClient.Incr(context.Background(), key).Err()
}

//判断key是否存在

func IsKey(key string) int64 {
	return global.RedisClient.Exists(context.Background(), key).Val()
}

// 添加有序集合
func CreateBook(key string, book *models.Book) error {
	mar, _ := json.Marshal(book)
	return global.RedisClient.ZAdd(context.Background(), key, redis.Z{
		Score:  float64(book.NumberOfComments),
		Member: mar,
	}).Err()
}

// 有序集合查询
func GetBook(key string) []string {
	return global.RedisClient.ZRange(context.Background(), key, 0, 10).Val()
}
